
package kaizone.haining.chaobohui.widget;

import java.util.List;

import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.R.color;
import kaizone.haining.chaobohui.json.Item;
import kaizone.haining.chaobohui.json.Item.PicModel;
import kaizone.haining.chaobohui.util.ImageFetcher;
import kaizone.haining.chaobohui.util.Utils;
import android.content.Context;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils.StringSplitter;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CBHItemHuodong extends LinearLayout {

    private TextView mHeadTextView;

    private TextView mTimeTextView;

    private TextView mLocalTextView;

    private TextView mRemarkTextView;

    private TextView mCountTextView01;

    private TextView mCountTextView02;

    private TextView mCountTextView03;

    private ImageView mImageView;

    private ImageFetcher mImageFetcher;

    private Item mItem;

    public CBHItemHuodong(Context context) {
        super(context);
        init();
    }

    public CBHItemHuodong(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {

    }

    public void setImageFetcher(ImageFetcher imageFetcher) {
        mImageFetcher = imageFetcher;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mHeadTextView = (TextView) findViewById(R.id.tv01);
        mTimeTextView = (TextView) findViewById(R.id.tv02);
        mLocalTextView = (TextView) findViewById(R.id.tv03);
        mRemarkTextView = (TextView) findViewById(R.id.tv04);
        mCountTextView01 = (TextView) findViewById(R.id.tv05);
        mCountTextView02 = (TextView) findViewById(R.id.tv06);
        mCountTextView03 = (TextView) findViewById(R.id.tv07);
        mImageView = (ImageView) findViewById(R.id.img);

        mCountTextView01.setVisibility(View.GONE);
    }

    public void setData(Item item) {
        mItem = item;
        dataToView();
    }

    private void dataToView() {
        mHeadTextView.setText(mItem.getName());

        StringBuilder timesb = new StringBuilder();
        String gmtactstart = Utils.subString(mItem.getGmtActivityStart(), "T");
        timesb.append(gmtactstart);
        timesb.append("~");
        String gmtactend = Utils.subString(mItem.getGmtActivityStart(), "T");
        timesb.append(gmtactend);
        mTimeTextView.setText(timesb);
        mLocalTextView.setText(mItem.getLocation());
        List<PicModel> picModels = mItem.getPicModelList();
        if (picModels != null && !picModels.isEmpty()) {
            PicModel picModel = picModels.get(0);
            String sum100x000 = picModel.sum100x000;
            if (picModel != null) {
                mImageFetcher.loadImage(sum100x000, mImageView);
            }
        }

        int checkinCount = mItem.getFollowCount();
        SpannableString gz02 = getSpannable(checkinCount + "");
        mCountTextView02.setText(gz02);
        mCountTextView02.append("人参加\t");

        int followCount = mItem.getFollowCount();
        SpannableString gz03 = getSpannable(followCount + "");
        mCountTextView03.setText(gz03);
        mCountTextView03.append("人关注\t");
    }

    public SpannableString getSpannable(String str) {
        SpannableString msp = new SpannableString(str);
        msp.setSpan(new AbsoluteSizeSpan(16, true), 0, str.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        int color = getResources().getColor(R.color.lightblue);
        msp.setSpan(new ForegroundColorSpan(color), 0, str.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        return msp;
    }
}
